﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WpfStarterKit.Framework.Navigation;

namespace WpfStarterKit.Framework.Commands
{
    /// <summary>
    /// A special type of RelayCommand that Navigates to another page right after it has run the Execute delegate
    /// </summary>
    public class NavigationCommand : RelayCommand
    {
        /// <summary>
        /// Navigation action which the NavigationCommand is supposed to execute
        /// </summary>
        protected string NavigationAction { get; set; }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="action">Navigation action to execute on the NavigationManager</param>
        public NavigationCommand(string action)
            : base(null, null)
        {
            NavigationAction = action;
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="action">Navigation action to execute on the NavigationManager</param>
        /// <param name="execute">Business code to execute passed in as an instance of the Action delegate</param>
        public NavigationCommand(string action, Action<object> execute)
            : base(execute, null)
        {
            NavigationAction = action;
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="action">Navigation action to execute on the NavigationManager</param>
        /// <param name="execute">Business code to execute passed in as an instance of the Action delegate</param>
        /// <param name="canExecute">Predicate that determines whether the command can be executed</param>
        public NavigationCommand(string action, Action<object> execute, Predicate<object> canExecute)
            : base(execute, canExecute)
        {
            NavigationAction = action;
        }

        /// <summary>
        /// Execute the action. Calls the base implementation followed by the navigation command
        /// </summary>
        /// <param name="parameter">Parameter passed in by the command source</param>
        public override void Execute(object parameter)
        {
            RunExecuteHandler(parameter);
            ApplicationManager.Instance.Navigation.Navigate(NavigationAction);
        }
    }
}
